﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Threading;
using System.Globalization;
public partial class _Default : System.Web.UI.Page
{

    CodeHelper codehelper = new CodeHelper();

    protected void Page_Load(object sender, EventArgs e)
    {
        Textarea1.Value = "";
        //this.AspNetJQGrid1.url = "handler/GridHandler.ashx";
        //AspNetJQGrid1.caption = "测试表头";
        //ProductModel pmode = new ProductModel();
        //pmode = CenterService.productService.GetProductByPcode("10002-20110622-230101");
        //Response.Buffer = true;
        //Response.ContentType = "application/ms-winword";
        //Response.Charset = "utf-8";
        //Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        //Response.AddHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmm") + ".doc");
        //Response.Write(WrodFileHelper.CreateWordFile(pmode).ToString());
        //// Response.Write();
        //// WrodFileHelper.ReplaceWord(pmode,WrodFileHelper.Doc2Text("d:\\download\\producttemplete.doc"));
        //Response.Flush();
        //Response.End();
        StringBuilder sbtable = new StringBuilder();
        if (!IsPostBack)
        {
            DataTable dataSet = codehelper.GetDBTables();
            drpTables.DataTextField = "name";
            drpTables.DataValueField = "name";
            drpTables.DataSource = dataSet;
            drpTables.DataBind();
            int i = 0;
            foreach (DataRow dr in dataSet.Rows)
            {
                i++;
                sbtable.AppendFormat("<li><input type='checkbox' id='{0}' value='{0}' name='chkTable'/>{0}</li>", dr["name"]);
                if (i % 5 == 0)
                {
                    sbtable.AppendFormat("<br/>");
                }

            }
            ltrTables.Text = sbtable.ToString();

            //chktable.DataTextField = "name";
            //chktable.DataValueField = "name";
            //chktable.DataSource = dataSet;
            //chktable.DataBind();

        }

    }
    //protected void Page_Error(object sender, EventArgs e)
    //{
    //    Exception ex = Server.GetLastError();
    //    if (ex is HttpRequestValidationException)
    //    {
    //        Response.Write("请您输入合法字符串。");
    //        Server.ClearError(); // 如果不ClearError()这个异常会继续传到Application_Error()。
    //    }
    //}
    protected void btnCreate_Click(object sender, EventArgs e)
    {







        //      string tableColumn="<Table Name="FactoryWareHouseReport_View">
        //  <Col Name="ID" SourceName="序号" Visible="False" IsKey="True"></Col>
        //  <Col Name="WarehouseName" SourceName="库房名称" IsGroupBy="true" ></Col>
        //  <Col Name="CarModel" SourceName="系列" ></Col>
        //  <Col Name="EngineModeNumber" SourceName="发动机"></Col>
        //  <Col Name="FixedNumberIdentification" SourceName="驾驶室"></Col>
        //  <Col Name="InnerCode" SourceName="内部管理代码"></Col>
        //  <Col Name="ContainerCatCode" SourceName="货箱"></Col>
        //  <Col Name="Color" SourceName="颜色"></Col>
        //  <Col Name="AvailableTotal" SourceName="可发"></Col>
        //  <Col Name="WillSendeTotal" SourceName="待发"></Col>
        //  <Col Name="OutRouteTotal" SourceName="在途"></Col>
        //  <Col Name="ConverstionPreAllocation" SourceName="待改"></Col>
        //  <Col Name="Total" SourceName="实存"></Col>
        //  <Col Name="FinanceTotal" SourceName="账存"></Col>
        //</Table>";


        string service = drpTables.SelectedValue.Replace(thead.Value, "");
        StringBuilder sb = new StringBuilder();
        StringBuilder sbcenter = new StringBuilder();
        string ss = drpTables.SelectedValue;
        if (!string.IsNullOrEmpty(Request["chkTable"]))
        {
            string[] arry = Request["chkTable"].Split(',');
            for (int i = 0; i < arry.Length; i++)
            {
                service = arry[i].Replace(thead.Value, "");
                ss = arry[i];
                switch (drpProType.SelectedValue)
                {
                    case "1":
                        codehelper.BulidSiteCode(service, ss, txtNameSpace.Value);
                        break;
                    case "2":
                        codehelper.BulidAppCode(service, ss, txtNameSpace.Value);
                        break;
                }
                string iservice = "I" + service + "Service";
                string Upservice = service + "Service";
                string lowservice = service.Substring(0, 1).ToLower() + service.Substring(1, service.Length - 1) + "Service";
                //   Textarea1.Value = "public static " + iservice + " " + drpTables.SelectedValue.Replace(thead.Value, "") + " = WebSingleton.ApplicationContainer.Resolve<" + iservice + ">();";
                sbcenter.Append("public static " + iservice + " " + lowservice + "{get;set;}\n");

                //  <IOCRegister id="sysMenuService" Iservice=" ERP.IService.ISysMenuService,ERP.IService" Service=" ERP.Service.ImpService.SysMenuService,ERP.Service" />

                sb.AppendFormat("<IOCRegister id=\"{0}\" IService=\"YXQ.IService.{1},YXQ.IService\" Service=\"YXQ.Service.{2},YXQ.Service\" />\n", lowservice, iservice, Upservice);



            }
            Textarea1.Value = sbcenter.ToString();
            txtXML.Value = sb.ToString();
            txtDao.Value = codehelper.dao.ToString();
            txtTableXML.Value = codehelper.tablexml.ToString();
            txtdetailaspx.Value = codehelper.detailaspx.ToString();
            txtdetailaspxcs.Value = codehelper.detailaspxcs.ToString();
            txtIservice.Value = codehelper.iservice.ToString();
            txtService.Value = codehelper.strservice.ToString();
            txtviewaspx.Value = codehelper.viewaspx.ToString();
            txtviewaspxcs.Value = codehelper.viewaspxcs.ToString();
            txtmodel.Value = codehelper.model.ToString();



            Response.Write("文件生成成功！");
        }


    }


}

